//
//  NewsViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/29.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "NewsViewController.h"
#import "AFNetworking.h"
#import "NewsTableViewCell.h"
#import "News.h"
#import "AnalystArticleViewController.h"
@interface NewsViewController ()

@property(nonatomic,retain)NSMutableArray *newsArray;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    self.newsArray = [NSMutableArray array];
    [super viewDidLoad];
    self.update=YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:nil] forCellReuseIdentifier:@"news"];
    
    // Do any additional setup after loading the view.
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)viewWillAppear:(BOOL)animated{
    
    if (self.update) {
        [self loadNewData];
    }
    self.update = NO;
}

#pragma mark- 下拉刷新（请求数据，结束header） -
-(void)loadNewData{
    
    NSString *requestAddress =
    @"http://newsapi.eastmoney.com/kuaixun/v1/yw_m_ywjh_20_1.html";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    // 不带参数的GET请求
    [manager GET:requestAddress
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"%@", responseObject);
             NSDictionary *resDic = [NSDictionary dictionary];
             resDic = responseObject;
             NSArray *array = [resDic objectForKey:@"LivesList"];
             for (NSDictionary *dic in array) {
                 
                 News *news = [[News alloc]init];
                 [news setValuesForKeysWithDictionary:dic];
                 [self.newsArray addObject:news];
                 
             }
             
             [self.tableView reloadData];
             [self.tableView.header endRefreshing];
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
             UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
             
             [alertView show];

             
         }];
    
    
    
}

#pragma mark- 上拉加载方法（请求数据，结束footer） -
-(void)loadMoreData{
    News *news = [self.newsArray lastObject];
    NSString *ids = news.newsid;
    NSString *requestAddress = [NSString stringWithFormat: @"http://newsapi.eastmoney.com/kuaixun/v1/yw_m_ywjh_20_min%@.html",ids];
   
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    // 不带参数的GET请求
    [manager GET:requestAddress
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"%@", responseObject);
             NSDictionary *resDic = [NSDictionary dictionary];
             resDic = responseObject;
             NSArray *array = [resDic objectForKey:@"LivesList"];
             for (NSDictionary *dic in array) {
                 
                 News *news = [[News alloc]init];
                 [news setValuesForKeysWithDictionary:dic];
                 [self.newsArray addObject:news];
                 
             }
             
             [self.tableView reloadData];
             [self.tableView.footer endRefreshing];
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
             
             [alertView show];

         }];
    
}




#pragma mark- 协议方法：分行 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.newsArray.count;
}


#pragma mark- 分区 -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}



#pragma mark- 协议方法cell复用 -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"news" forIndexPath:indexPath];
    News *news = [self.newsArray objectAtIndex:indexPath.row];
    cell.news = news;
    
    if (news.picImage ==nil) {
        
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:news.image] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
             news.picImage = cell.picImageView.image;
            
        }];
        
    }
    
    cell.picImageView.image = news.picImage;

    return cell;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 108;
}


#pragma mark- cell被选中的方法 -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AnalystArticleViewController *anlysVC = [[AnalystArticleViewController alloc]init];
    News *news = [self.newsArray objectAtIndex:indexPath.row];
    
    NSString *url = [NSString stringWithFormat:@"%@",news.url_m];
    anlysVC.webUrl = url;
    
    [self.navigationController pushViewController:anlysVC animated:YES];
    
}
 






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
